//
//  TableView_RQ_SPF_ZJQ.m
//  PlayBall
//
//  Created by Jdd‘s macbook on 13-10-29.
//
//

#import "JddRQ_SPF_ZJQ_TableView.h"

@interface JddRQ_SPF_ZJQ_TableView ()

@end

@implementation JddRQ_SPF_ZJQ_TableView
-(void)viewDidLoad
{
    [super viewDidLoad];
    //self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,0)];
    self.tableView.tableFooterView = v;

    [self.tableView setSeparatorColor:SeparatorLineColor];
    if (IOS7) {
        [self.tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    groupArray = [[NSMutableArray alloc] init];
    for (int i=0; i<[_tableData count]; ++i) {
        JddFollowNumber *follownum = [[JddFollowNumber alloc]init];
        follownum.open = 1;
        [groupArray addObject:follownum];
    }    
}

-(void)sportTeamButtonHeadClicked:(NSNumber*)position{
    JddFollowNumber *groupDic = [groupArray objectAtIndex:[position intValue]];
    //设置groupDic中的展开状态
    if(groupDic.open ==0){
        groupDic.open=1;
    }else {
        groupDic.open = 0;
    }
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:[position intValue]]
                  withRowAnimation:UITableViewRowAnimationFade];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return [_tableData count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSDictionary* sectionCell = [_tableData objectAtIndex:section];
    NSString *MCount = [sectionCell objectForKey:@"MCount"];
    if ([groupArray count]>0) {
        JddFollowNumber *groupDic = [groupArray objectAtIndex:section];
        if (groupDic.open==0) {
            return 0;
            [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
        }else {
            return [MCount intValue];
        }
        
    }else {
        return [MCount intValue];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.lottId == LOTTERY_PLAY_METHOD_FOOTBALL_ZJQ) {
        return 92.0;
    }
    return 75.0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 34.0;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    JddPickNumSportItemCellHeader *cell = [JddPickNumSportItemCellHeader getInstanceView];
    NSDictionary* sectionCell = [_tableData objectAtIndex:section];
    NSNumber *MCount = [sectionCell objectForKey:@"MCount"];
    NSString *Name = [sectionCell objectForKey:@"issue"];
    cell.day.text = Name;
    cell.Date.text = [JddCommonFunction convertWeekByNumber:[sectionCell objectForKey:@"Wk"]];
    cell.Mnum.text = [NSString stringWithFormat:@"%d场比赛可投",[MCount intValue]];
    cell.statePosition = [NSNumber numberWithInt:section];
    cell.delegate = self;
    if ([groupArray count]>0) {
        JddFollowNumber *groupDic = [groupArray objectAtIndex:section];
        if (groupDic.open==0) {
            cell.HeadButton.image = [UIImage imageNamed:@"hemai_hall_down.png"];
        }else {
            cell.HeadButton.image = [UIImage imageNamed:@"hemai_up.png"];
        }
        
    }
    return cell;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary* sectionCell = [_tableData objectAtIndex:indexPath.section];
    NSArray *Match = [sectionCell objectForKey:@"Matches"];
    NSDictionary *MatchCell = [Match objectAtIndex:indexPath.row];
    NSString *ETime = [MatchCell objectForKey:@"ETime"];
    NSString *HTeam = [MatchCell objectForKey:@"HTeam"];
    NSString *MID = [MatchCell objectForKey:@"MID"];
    NSMutableString *NMm = [MatchCell objectForKey:@"NMm"];
   [NMm stringByReplacingOccurrencesOfString:@"联赛" withString:@"fwefw"];
    NSNumber *Rq = [MatchCell objectForKey:@"Rq"];
    NSString *VTeam = [MatchCell objectForKey:@"VTeam"];
    NSNumber *isSingle = [MatchCell objectForKey:@"IsSingle"];
    JddPickNumSportItemCellParent *cell;
    //胜平负/让球胜平负
    if (self.lottId == LOTTERY_PLAY_METHOD_FOOTBALL || self.lottId == LOTTERY_PLAY_METHOD_FOOTBALL_SPF) {
        cell = [JddPickNumSportItemCell getInstanceView];
        if([isSingle intValue] == 0){
            [cell.label_isSingle setHidden:YES];
        }else{
            [cell.label_isSingle setHidden:NO];
        }
        NSString *SPSPF = [MatchCell objectForKey:@"SpRQSPF"];
        //类型为胜平负时取字段SpSPF
        if (self.lottId == LOTTERY_PLAY_METHOD_FOOTBALL_SPF) {
            SPSPF = [MatchCell objectForKey:@"SpSPF"];
        }
        NSArray *Pl =[SPSPF componentsSeparatedByString:@"|"];
        //类型为让球胜平负，显示让球
        if (!Rq == 0 && self.lottId == LOTTERY_PLAY_METHOD_FOOTBALL) {
            cell.Rq.text = [NSString stringWithFormat:@"(%d)",[Rq intValue]];
            cell.vs.hidden = YES;
        }
        NSString *sStr = @"-";
        NSString *pStr = @"-";
        NSString *fStr = @"-";
        cell.Win.enabled = NO;
        cell.Drew.enabled = NO;
        cell.Lose.enabled = NO;

        if (Pl.count > 2 ) {
            sStr = [NSString stringWithFormat:@"胜%1.2f", [[Pl objectAtIndex:0] floatValue]];
            pStr = [NSString stringWithFormat:@"平%1.2f", [[Pl objectAtIndex:1] floatValue]];
            fStr = [NSString stringWithFormat:@"负%1.2f", [[Pl objectAtIndex:2] floatValue]];
            cell.Win.enabled = YES;
            cell.Drew.enabled = YES;
            cell.Lose.enabled = YES;
        }
        
        [cell.Win setTitle:sStr forState:UIControlStateNormal];
        [cell.Drew setTitle:pStr forState:UIControlStateNormal];
        [cell.Lose setTitle:fStr forState:UIControlStateNormal];
    }
    
    //总进球
    if (self.lottId == LOTTERY_PLAY_METHOD_FOOTBALL_ZJQ) {
        cell = [JddPickNumSportItemZJQCell getInstanceView];
        NSArray *zjq = [[MatchCell objectForKey:@"SpZJQ"] componentsSeparatedByString:@"|"];
        [cell.btn1 setTitle:[zjq objectAtIndex:0] forState:UIControlStateNormal];
        [cell.btn2 setTitle:[zjq objectAtIndex:1] forState:UIControlStateNormal];
        [cell.btn3 setTitle:[zjq objectAtIndex:2] forState:UIControlStateNormal];
        [cell.btn4 setTitle:[zjq objectAtIndex:3] forState:UIControlStateNormal];
        [cell.btn5 setTitle:[zjq objectAtIndex:4] forState:UIControlStateNormal];
        [cell.btn6 setTitle:[zjq objectAtIndex:5] forState:UIControlStateNormal];
        [cell.btn7 setTitle:[zjq objectAtIndex:6] forState:UIControlStateNormal];
        [cell.btn8 setTitle:[zjq objectAtIndex:7] forState:UIControlStateNormal];
        
        for(JddSportNumber *num in self.sportlotterylist.sportList){
            if ([num.code isEqualToString:MID]) {
                for (NSString *num2 in num.teamArray) {
                    NSLog(@"%@", num2 );
                    NSLog(@"%@", [zjq objectAtIndex:0]);
                    if ([num2 isEqualToString:@"0"]){
                        [cell.btn1 setSelected:YES];
                    }else if ([num2 isEqualToString:@"1"]){
                        [cell.btn2 setSelected:YES];
                    }else if ([num2 isEqualToString:@"2"]){
                        [cell.btn3 setSelected:YES];
                    }else if ([num2 isEqualToString:@"3"]){
                        [cell.btn4 setSelected:YES];
                    }else if ([num2 isEqualToString:@"4"]){
                        [cell.btn5 setSelected:YES];
                    }else if ([num2 isEqualToString:@"5"]){
                        [cell.btn6 setSelected:YES];
                    }else if ([num2 isEqualToString:@"6"]){
                        [cell.btn7 setSelected:YES];
                    }else if ([num2 isEqualToString:@"7"]){
                        [cell.btn8 setSelected:YES];
                    }
                }
            }
        }
    }
    for(JddSportNumber *num in self.sportlotterylist.sportList){
        if ([num.code isEqualToString:MID]) {
            for (NSNumber *num2 in num.teamArray) {
                switch ([num2 intValue]) {
                    case 3:
                        [cell.Win setSelected:YES];
                        break;
                    case 1:
                        [cell.Drew setSelected:YES];
                        break;
                    case 0:
                        [cell.Lose setSelected:YES];
                        break;
                    default:
                        break;
                }
            }
        }
    }
    cell.weakNum.text = [NSString stringWithFormat:@"%@",[MID substringFromIndex:6]];
    //截止
    cell.EndTime.text = [NSString stringWithFormat:@"%@",[ETime substringWithRange:NSMakeRange(11,5)]];
    cell.endtime = ETime;
    cell.Match.text = NMm;
    cell.HTeam.text= HTeam;
    cell.VTeam.text = VTeam;
    cell.MID = MID;
    
    cell.aSportNumber = [[JddSportNumber alloc] init];
    cell.bSportNumber = [[JddSportNumber alloc] init];
    cell.cSportNumber = [[JddSportNumber alloc] init];
    cell.cSportNumber.code = MID;
    cell.cSportNumber.Hteam = HTeam;
    cell.cSportNumber.Vteam = VTeam;
    cell.cSportNumber.endTime = ETime;
    cell.cSportNumber.isSingle = [isSingle intValue];
    
    cell.cSportNumber.teamArray= [[NSMutableArray alloc] init];
    cell.bSportNumber.code = MID;
    cell.bSportNumber.Hteam = HTeam;
    cell.bSportNumber.Vteam = VTeam;
    cell.bSportNumber.endTime = ETime;
    cell.bSportNumber.isSingle = [isSingle intValue];
    
    cell.bSportNumber.teamArray= [[NSMutableArray alloc] init];
    cell.aSportNumber.code = MID;
    cell.aSportNumber.Hteam = HTeam;
    cell.aSportNumber.Vteam = VTeam;
    cell.aSportNumber.endTime = ETime;
    cell.aSportNumber.isSingle = [isSingle intValue];
    
    cell.aSportNumber.teamArray= [[NSMutableArray alloc] init];
    cell.delegate = self;
    return cell;
}

-(void)sportTeamButtonClicked:(id)sender number:(JddSportNumber*) number1{
    UIButton *button = sender;
    JddSportNumber *number = [[JddSportNumber alloc] init];
    number.code = number1.code;
    number.teamArray = [NSMutableArray arrayWithArray:number1.teamArray];
    number.endTime = number1.endTime;
    number.Hteam= number1.Hteam;
    number.Vteam = number1.Vteam;
    number.isSingle = number1.isSingle;

    if (![self.sportlotterylist BallHasNumber:number]) {
        [button setSelected:NO];
    }else {
        if ([self.sportlotterylist.sportList count]>15) {
            [self.sportlotterylist BallHasNumber:number];
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"最多选择15场比赛" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
            [button setSelected:NO];
        }else {
            [button setSelected:YES];
        }
        
    }
    [self.delegate performSelector:@selector(selectMatch)];
}
@end
